package cn.project.lemontree.controller;

import cn.project.lemontree.bean.Online_source;
import cn.project.lemontree.service.Online_sourceService;
import cn.project.lemontree.tools.CommonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/source")
@Api(tags = "资源管理模块")
@CrossOrigin  //跨域访问
public class Online_sourceController {
    @Resource
    private Online_sourceService onlineSourceService;

    @ApiOperation("分页查询线上资源信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码",required=true,dataTypeClass = int.class,example = "1"),
            @ApiImplicitParam(name="size",value="每页行数",required=true,dataTypeClass = int.class,example = "10"),
            @ApiImplicitParam(name="topic",value="主题",required=false,dataTypeClass = String.class)
    })
    @GetMapping("/listpage")
    public CommonResult listpage(int page,int size,String topic){
        Map<String ,Object> map = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        if(topic!=null || !topic.equals(topic))
            queryWrapper.like("topic",topic);
        map.put("data",onlineSourceService.page(new Page<>(page,size),queryWrapper).getRecords());
        map.put("count",onlineSourceService.count());
        map.put("page",page);
        return CommonResult.success(map);
    }
    @ApiOperation("普通添加线上资源")
    @PostMapping("/add")
    public CommonResult add(@RequestBody Online_source onlineSource){
        return CommonResult.success(onlineSourceService.add(onlineSource));
    }

}
